﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.CMS.Process;
using TjZheng.Controls;
using TjZheng.Framework;
using TjZheng.Test.Business;
using TjZheng.Test.DataEntity;

namespace TjZheng.CMS.Controllers
{
    public class TemplateController : CMSController
    {
        BizTemplateTest objBizTemplateTest { get; set; }
        public TemplateController()
        {
            objBizTemplateTest = new BizTemplateTest();
        }

        public ActionResult Index()
        {
            return View();
        }

        //[QueryFilter]
        [QueryMsSqlFilter]
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult ListJson(int page, int rows)
        {
            //return Json(objBizTemplateTest.GetPageResult(page - 1, rows, QueryResult.Condition, "Id")); 
            return Json(objBizTemplateTest.GetPageResult(page - 1, rows, QueryResult.Condition, QueryResult.Parameters, "Id"));
        }

        public ActionResult EditPage(long? id)
        {
            TemplateTest objTemplateTest = null;
            if (id.HasValue)
            {
                objTemplateTest = objBizTemplateTest.GetRecord(id.Value);
            }
            else
            {
                objTemplateTest = new TemplateTest();
            }
            return View(objTemplateTest);
        }

        //[ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Save(TemplateTest objProvide)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objProvide == null)
            {
                objInvokeResult.ResultCode = "ProvideNull";
                objInvokeResult.ResultMessage = "保存失败";
                return Json(objInvokeResult, "text/plain");
            }
            TemplateTest objTarget = null;
            if (objProvide.Id <= 0)
            {
                objTarget = new TemplateTest();
                Save(objTarget, objProvide);
                objBizTemplateTest.Add(objTarget);
            }
            else
            {
                objTarget = objBizTemplateTest.GetRecord(objProvide.Id);
                if (objTarget == null)
                {
                    objInvokeResult.ResultCode = "DataNull";
                    objInvokeResult.ResultMessage = "保存失败";
                    return Json(objInvokeResult, "text/plain");
                }
                Save(objTarget, objProvide);
                objBizTemplateTest.Update(objTarget);
            }

            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "保存成功";
            return Json(objInvokeResult, "text/plain");
        }

        public ActionResult Delete(string id)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (id.IsNull())
            {
                objInvokeResult.ResultCode = "IDNull";
                objInvokeResult.ResultMessage = "请先选择一条数据";
                return Json(objInvokeResult, "text/plain");
            }
            objBizTemplateTest.DeleteIDString(id);
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "删除成功";
            return Json(objInvokeResult, "text/plain");
        }

        void Save(TemplateTest objTarget, TemplateTest objProvide)
        {
            objTarget.TestString = objProvide.TestString.CutWord(256);
            objTarget.TestBool = objProvide.TestBool;
            objTarget.TesInt = objProvide.TesInt;
            objTarget.TestDouble = objProvide.TestDouble;
            objTarget.TestDate = objProvide.TestDate;
            objTarget.TestImage = objProvide.TestImage.CutWord(256);
            objTarget.TestImages = objProvide.TestImages;
            objTarget.TestFile = objProvide.TestFile.SaveFiles("objProvide.TestFile");
            objTarget.TestCheckBoxList = objProvide.TestCheckBoxList.CutWord(64);
            objTarget.TestRadioList = objProvide.TestRadioList.CutWord(64);
            objTarget.TestCheckBoxListRemote = objProvide.TestCheckBoxListRemote.CutWord(64);
            objTarget.TestRadioButtonListRemote = objProvide.TestRadioButtonListRemote.CutWord(64);
            objTarget.TestComboBoxRemote = objProvide.TestComboBoxRemote;
            objTarget.TestArea = objProvide.TestArea;
            objTarget.TestContent = objProvide.TestContent;
        }

        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult TestListJson()
        {
            return Json(new InvokeResult
            {
                Data = new List<SelectListItem> 
                {
                    new SelectListItem{Text="测试1",Value="1"},
                    new SelectListItem{Text="测试2",Value="2"},
                    new SelectListItem{Text="测试3",Value="3"},
                    new SelectListItem{Text="测试4",Value="4"},
                    new SelectListItem{Text="测试5",Value="5"}
                }
            });
        }
    }
}
